package top.jbxie.raft.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.jbxie.raft.service.IPService;
import top.ninwoo.bishe.starter.service.NetworkService;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

@Service
public class IPServiceImpl implements IPService {

    @Autowired
    private NetworkService networkService;

    @Override
    public String getSelfIp() throws UnknownHostException {
        InetAddress addr = Inet4Address.getLocalHost();
        return addr.getHostAddress() + ":8775";
    }

    @Override
    public String[] getIpList(Long clusterId, String appName) {
        List<String> ipListS = networkService.getIpListByAppName(clusterId, appName);
        int n = ipListS.size();
        String[] ipList = new String[n];
        for (int i = 0; i < n; i++) {
            if(!ipListS.isEmpty()){
                String ip_tmp = ipListS.get(i);
                String[] split_list = ip_tmp.split("/");
                ipList[i] = split_list[0]+":8775";
            }
        }
        return ipList;
    }

}
